#!/usr/bin/env groovy
// MILESTONE部署到测试工作站
pipeline {
    agent none
    parameters {
        choice(name: 'db', choices: ['tgraph_kernel', 'postgresql', 'mariadb', 'neo4j1', 'neo4j2'], description: 'The DB to Test')
        choice(name: 'dataset', choices: ['energy', 'traffic', 'syn'], description: 'Datasets')
        string(name:'tp_size', defaultValue: 'energy7d_20120101-20120108', description: 'temporal data size')
    }
    tools {
        maven 'maven'
        jdk   'jdk8'
    }
    environment {
        MILESTONE_NAME = "milestone_${params.db}_${params.tp_size}"
    }
    stages {
        stage('Deploy milestone') {
            parallel {
                stage('Server243'){
                    agent {
                        label '243workstation'
                    }
                    when {
                        beforeAgent true
                        allOf {
                            expression { params.db != 'postgresql' }
                            expression { params.db != 'mariadb' }
                        }
                    }
                    steps {
                        bat 'net use \\\\192.168.1.183\\jenkins langduhua /user:song'
                        dir("E:\\tgraph\\jenkins-workspace"){
                            bat "xcopy \\\\192.168.1.183\\jenkins\\${MILESTONE_NAME} . /Y"
                            bat "if exist ${params.db} ( rmdir /q /s ${params.db} )"
                            unzip zipFile: "${MILESTONE_NAME}", dir: "${params.db}"
                        }
                    }
                }
                stage('Server199'){
                    agent {
                        label '199workstation'
                    }
                    when {
                        beforeAgent true
                        anyOf {
                            expression { params.db == 'postgresql' }
                            expression { params.db == 'mariadb' }
                        }
                    }
                    environment {
                        MARIADB_WORKSPACE = "E:\\Program Files\\MariaDB 10.6\\data"
                        PGPASSWORD = "langduhua"
                    }
                    steps {
                        bat 'net use \\\\192.168.1.183\\jenkins langduhua /user:song'
                        bat "xcopy \\\\192.168.1.183\\jenkins\\${MILESTONE_NAME} . /Y" 
                        script {
                            // 恢复milestone状态为指定内容
                            switch (params.db) {
                                case 'postgresql':
                                    bat "echo DROP DATABASE IF EXISTS ${params.dataset}; | psql -Upostgres"
                                    bat "createdb.exe -Upostgres ${params.dataset}"
                                    bat "pg_restore.exe -Upostgres -e -d ${params.dataset} ${env.MILESTONE_NAME}"
                                    break
                                case 'mariadb':
                                    bat "echo DROP DATABASE IF EXISTS ${params.dataset}; | mysql -u root -plangduhua"
                                    unzip zipFile: "${MILESTONE_NAME}", dir: "${env.MILESTONE_NAME}_d"
                                    bat "mariabackup.exe --prepare --target-dir=${env.MILESTONE_NAME}_d --user=root"
                                    // 还原数据（删除datadir目录中与之冲突的文件即可）
                                    bat "net stop MariaDB"
                                    dir(env.MARIADB_WORKSPACE){
                                        bat "del aria_log*"
                                        bat "del ib*"
                                        bat "del x*"
                                        bat "if exist ${params.db} ( rmdir /q /s ${params.db} )"
                                    }
                                    bat "mariabackup.exe --copy-back --force-non-empty-directories --target-dir=${env.MILESTONE_NAME}_d --user=root"
                                    bat "net start MariaDB"
                                    break
                            }
                        }
                    }
                    post{
                        failure {
                            script{
                                try{
                                    bat "net start MariaDB"
                                }catch(Exception e){
                                    echo 'MariaDB started'
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}